using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Plugin;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace MicroMsg.UI.Page
{
	public class TXBlogReadView : ChatContainerPage
	{
		public class SessionICommand : ICommand
		{
			public delegate void DelGroupMsg(TXNews item);
			private bool _canExecute = true;
			public event TXBlogReadView.SessionICommand.DelGroupMsg delmsg;
			public event EventHandler CanExecuteChanged;
			public bool IsEnable
			{
				get
				{
					return this._canExecute;
				}
				set
				{
					if (this._canExecute != value)
					{
						EventHandler canExecuteChanged = this.CanExecuteChanged;
						if (canExecuteChanged != null)
						{
							canExecuteChanged.Invoke(this, EventArgs.Empty);
						}
					}
					this._canExecute = value;
				}
			}
			public bool CanExecute(object parameter)
			{
				return this._canExecute;
			}
			public void Execute(object parameter)
			{
				TXNews tXNews = parameter as TXNews;
				if (tXNews == null)
				{
					return;
				}
				if (MainPage.mainPageInstance == null)
				{
					return;
				}
				this.delmsg(tXNews);
			}
		}
		private const string TAG = "TXBlogReadView";
		private const string ScrollingState = "Scrolling";
		private const string NotScrollingState = "NotScrolling";
		private MMObservableCollection<TXNews, TXBlogItem> mDataList = new MMObservableCollection<TXNews, TXBlogItem>();
		private EventWatcher mListChangeWatcher;
		private int mPositon;
		private TXBlogReadView.SessionICommand mCommand;
		private ScrollViewer mScrollViewer;
		private bool mBIsScrollHandlerSet;
		private double oldOffset;
		private Point pointLeave;
		private Point pointEnter;
		private ApplicationBar mBar;
		private ApplicationBarMenuItem pluginSettingButton;
		private List<UIElement> mUiToHideList;
		internal Grid LayoutRoot;
		internal Grid LayoutContent;
		internal panelTitle2Line panelTitle;
		internal Canvas canvasProgressBar;
		internal PerformanceProgressBar ProgressBar;
		internal Grid listView;
		internal StackPanel ContentPanel;
		internal MMListBox Sessionlist;
		internal Grid NoMemberView;
		private bool _contentLoaded;
		public bool IsScrolling
		{
			get;
			private set;
		}
		public TXBlogReadView()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.TXBlogReadView_loaded));
			base.add_Unloaded(new RoutedEventHandler(this.TXBlogReadView_unloaded));
		}
		private void TXBlogReadView_loaded(object sender, RoutedEventArgs e)
		{
			this.setScrollHandler();
		}
		private void TXBlogReadView_unloaded(object sender, RoutedEventArgs e)
		{
		}
		protected override void OnPageIn(object[] object_0)
		{
			this.InitTXBlogReadView();
			this.InitWatcher();
			Touch.add_FrameReported(new TouchFrameEventHandler(this.Touch_FrameReported));
		}
		private void InitWatcher()
		{
			if (this.mListChangeWatcher == null)
			{
				this.mListChangeWatcher = new EventWatcher(this, this, new EventHandlerDelegate(this.OnMsgChange));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CHAT_MSG_ADD, this.mListChangeWatcher);
			}
		}
		private void UnitWatcher()
		{
			if (this.mListChangeWatcher != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CHAT_MSG_ADD, this.mListChangeWatcher);
				this.mListChangeWatcher = null;
			}
		}
		private void OnMsgChange(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
			List<ChatMsg> listObject = evtArgs.getListObject<ChatMsg>();
			if (listObject != null && listObject.get_Count() > 0)
			{
				using (List<ChatMsg>.Enumerator enumerator = listObject.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ChatMsg current = enumerator.get_Current();
						if (ContactHelper.isBlogAppContact(current.strTalker))
						{
							this.addGroupMsg(current);
						}
					}
				}
				return;
			}
		}
		private void InitTXBlogReadView()
		{
			base.set_ApplicationBar(this.GetApplicationBar());
			this.mDataList.Bind(this.Sessionlist);
			ConversationMgr.setUnreadCount(ConstantValue.TAG_BLOGAPP, 0, false);
			this.mPositon = StorageMgr.chatMsg.getMsgNum(ConstantValue.TAG_BLOGAPP);
			if (this.mPositon <= 0)
			{
				this.ChangeToNoMsgView();
				return;
			}
			this.getGroupMsg();
			this.ChangeToListView();
		}
		private void SessionItem_ItemClick(object sender, object object_0)
		{
			TXNews tXNews = object_0 as TXNews;
			if (tXNews == null)
			{
				return;
			}
			if (tXNews.isCreateTime)
			{
				return;
			}
			if (tXNews.isBlankItem)
			{
				return;
			}
			ToolFunction.GotoPageForUserControl("/Source/Plugin/TXBlogRead/UI/TXBlogReadDetail.xaml", new object[]
			{
				tXNews,
				this.mDataList
			}, false);
		}
		private TXBlogReadView.SessionICommand getCommand()
		{
			if (this.mCommand == null)
			{
				this.mCommand = new TXBlogReadView.SessionICommand();
			}
			return this.mCommand;
		}
		private void DeleteSessionMenu_Loaded(object sender, RoutedEventArgs e)
		{
			MenuItem menuItem = sender as MenuItem;
			if (menuItem != null && menuItem.CommandParameter != null)
			{
				object arg_18_0 = menuItem.CommandParameter;
				menuItem.Command = this.getCommand();
				this.mCommand.delmsg += new TXBlogReadView.SessionICommand.DelGroupMsg(this.delGroupMsgByItem);
				return;
			}
		}
		private void DeleteSessionMenu_Unloaded(object sender, RoutedEventArgs e)
		{
			this.mCommand.delmsg -= new TXBlogReadView.SessionICommand.DelGroupMsg(this.delGroupMsgByItem);
		}
		private void setScrollHandler()
		{
			if (this.mBIsScrollHandlerSet)
			{
				return;
			}
			this.mBIsScrollHandlerSet = true;
			this.mScrollViewer = (ScrollViewer)UIElementTraveler.FindElementRecursive(this.Sessionlist, typeof(ScrollViewer));
			if (this.mScrollViewer == null)
			{
				return;
			}
			FrameworkElement frameworkElement = VisualTreeHelper.GetChild(this.mScrollViewer, 0) as FrameworkElement;
			if (frameworkElement != null)
			{
				VisualStateGroup visualStateGroup = UIElementTraveler.FindVisualState(frameworkElement, "ScrollStates");
				if (visualStateGroup != null)
				{
					visualStateGroup.add_CurrentStateChanging(new EventHandler<VisualStateChangedEventArgs>(this.OnScrollStateChanging));
					visualStateGroup.add_CurrentStateChanged(new EventHandler<VisualStateChangedEventArgs>(this.OnScrollStateChanged));
				}
			}
		}
		private void OnScrollStateChanged(object sender, VisualStateChangedEventArgs e)
		{
			if (this.ProgressBar.IsIndeterminate)
			{
				this.ProgressBar.IsIndeterminate = false;
			}
		}
		private void OnScrollStateChanging(object sender, VisualStateChangedEventArgs e)
		{
			this.IsScrolling = (e.get_NewState().get_Name() == "Scrolling");
			if (e.get_NewState().get_Name() == "NotScrolling")
			{
				if (this.mScrollViewer != null)
				{
					this.mScrollViewer.ScrollToVerticalOffset(this.mScrollViewer.get_VerticalOffset());
				}
				int arg_5B_0 = this.mDataList.get_Count();
				this.tryGetHistoryMsg();
			}
		}
		private void Touch_FrameReported(object sender, TouchFrameEventArgs e)
		{
			TouchPointCollection touchPoints = e.GetTouchPoints(null);
			if (1 != touchPoints.get_Count())
			{
				try
				{
					TouchPoint primaryTouchPoint = e.GetPrimaryTouchPoint(null);
					if (primaryTouchPoint != null)
					{
						this.TouchLeave(primaryTouchPoint.get_Position());
					}
				}
				catch (Exception)
				{
					return;
				}
			}
			TouchPoint touchPoint = null;
			try
			{
				touchPoint = e.GetPrimaryTouchPoint(null);
			}
			catch (Exception)
			{
				return;
			}
			if (touchPoint != null)
			{
				if (touchPoint.get_Action() == 1)
				{
					this.TouchEnter(touchPoint.get_Position());
					return;
				}
				if (touchPoint.get_Action() == 2)
				{
					return;
				}
				if (touchPoint.get_Action() == 3)
				{
					this.TouchLeave(touchPoint.get_Position());
				}
				return;
			}
		}
		private void TouchLeave(Point point)
		{
			this.pointLeave = point;
		}
		private void TouchEnter(Point point)
		{
			this.pointEnter = point;
		}
		private double getScrollDistance()
		{
			return this.pointLeave.get_Y() - this.pointEnter.get_Y();
		}
		private void delGroupMsgByItem(TXNews item)
		{
			ChatMsg msg = StorageMgr.chatMsg.getMsg(item.nChatMsgLocalID);
			StorageMgr.chatMsg.delMsg(msg);
			if (msg == null)
			{
				DebugEx.debugBreak();
			}
			for (int i = this.mDataList.get_Count() - 1; i >= 0; i--)
			{
				if (this.mDataList[i].nChatMsgLocalID == item.nChatMsgLocalID)
				{
					this.mDataList.RemoveAt(i);
				}
			}
			if (this.mDataList.get_Count() == 0)
			{
				if (this.getGroupMsg() == 0)
				{
					this.ChangeToNoMsgView();
					return;
				}
			}
			else
			{
				this.mDataList[0].hasMoreItem = true;
				this.mDataList[0] = this.mDataList[0];
			}
		}
		private int addGroupMsg(ChatMsg chatMsg_0)
		{
			List<TXNews> list = BlogConversationMgr.convertChatMsg2BlogMsgList(chatMsg_0);
			if (list != null && list.get_Count() > 0)
			{
				TXNews tXNews = new TXNews();
				tXNews.isCreateTime = true;
				tXNews.hasMoreItem = false;
				tXNews.strSource = Util.formatTimeInChatMsg(chatMsg_0.nCreateTime);
				tXNews.nChatMsgLocalID = list.get_Item(0).nChatMsgLocalID;
				this.mDataList.Add(tXNews);
				using (List<TXNews>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						TXNews current = enumerator.get_Current();
						this.mDataList.Add(current);
					}
				}
				TXNews tXNews2 = new TXNews();
				tXNews2.isBlankItem = true;
				tXNews2.hasMoreItem = false;
				tXNews2.nChatMsgLocalID = list.get_Item(0).nChatMsgLocalID;
				this.mDataList.Add(tXNews2);
				this.mDataList[0].hasMoreItem = true;
				this.ChangeToListView();
				return list.get_Count();
			}
			return 0;
		}
		private int getGroupMsg()
		{
			if (this.mPositon <= 0)
			{
				return 0;
			}
			List<TXNews> list = null;
			string strSource = "";
			if (!BlogConversationMgr.getGroupMsg(this.mPositon, ref strSource, ref list))
			{
				return 0;
			}
			this.mPositon--;
			if (this.mDataList.get_Count() > 0)
			{
				this.mDataList[0].hasMoreItem = false;
				this.mDataList[0] = this.mDataList[0];
			}
			int num = 0;
			TXNews tXNews = new TXNews();
			tXNews.isCreateTime = true;
			tXNews.strSource = strSource;
			tXNews.nChatMsgLocalID = list.get_Item(0).nChatMsgLocalID;
			this.mDataList.Insert(0, tXNews);
			num = 0 + 1;
			using (List<TXNews>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					TXNews current = enumerator.get_Current();
					this.mDataList.Insert(num, current);
					num++;
				}
			}
			TXNews tXNews2 = new TXNews();
			tXNews2.isBlankItem = true;
			tXNews2.hasMoreItem = false;
			tXNews2.nChatMsgLocalID = list.get_Item(0).nChatMsgLocalID;
			this.mDataList.Insert(num, tXNews2);
			this.mDataList[0].hasMoreItem = true;
			return list.get_Count();
		}
		private int tryGetHistoryMsg()
		{
			if (this.mScrollViewer == null || this.mDataList == null)
			{
				return 0;
			}
			Log.smethod_5("TXBlogReadView", "tryGetHistoryNews start");
			if ((this.oldOffset - 0.0001 <= 0.0 && this.getScrollDistance() - 150.0 > 0.0) || (this.mScrollViewer.get_VerticalOffset() - 1E-06 <= 0.0 && this.getScrollDistance() - 600.0 > 0.0))
			{
				this.ProgressBar.IsIndeterminate = true;
				this.oldOffset = this.mScrollViewer.get_VerticalOffset();
				Deployment.get_Current().get_Dispatcher().BeginInvoke(delegate
				{
					this.getGroupMsg();
				});
				return 0;
			}
			this.oldOffset = this.mScrollViewer.get_VerticalOffset();
			return 0;
		}
		public ApplicationBar GetApplicationBar()
		{
			if (this.mBar != null)
			{
				if (!this.mBar.get_IsVisible())
				{
					this.mBar.set_IsVisible(true);
				}
				return this.mBar;
			}
			this.mBar = new ApplicationBar();
			this.mBar.set_IsMenuEnabled(true);
			this.mBar.set_IsVisible(true);
			if (this.mBar != null)
			{
				this.mBar.set_BackgroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_BK_BRUSH).get_Color());
				this.mBar.set_ForegroundColor(((SolidColorBrush)R.theme.COMMON_APPBAR_FG_BRUSH).get_Color());
			}
			this.pluginSettingButton = new ApplicationBarMenuItem(strings.Set_Plugin_Setting);
			this.pluginSettingButton.add_Click(new EventHandler(this.pluginSettingButton_Click));
			this.mBar.get_MenuItems().Add(this.pluginSettingButton);
			return this.mBar;
		}
		private void pluginSettingButton_Click(object sender, EventArgs e)
		{
			base.GoToPage("/Source/Plugin/PluginSettingPage.xaml", new object[]
			{
				PluginManager.findPluginByName("TXBlogRead")
			}, false);
			int i = 0;
			while (i < PageManager.getInstance().basePageList.get_Count())
			{
				if (!(PageManager.getInstance().basePageList.get_Item(i) is PluginSettingPage))
				{
					i++;
				}
				else
				{
					PluginSettingPage pluginSettingPage = PageManager.getInstance().basePageList.get_Item(i) as PluginSettingPage;
					if (pluginSettingPage.GetPluginName() == "TXBlogRead")
					{
						PageManager.getInstance().PopPage();
						return;
					}
					return;
				}
			}
		}
		private void ChangeToNoMsgView()
		{
			if (this.listView.get_Visibility() == 1)
			{
				return;
			}
			this.listView.set_Visibility(1);
			this.NoMemberView.set_Visibility(0);
		}
		private void ChangeToListView()
		{
			if (this.NoMemberView.get_Visibility() == 1)
			{
				return;
			}
			this.NoMemberView.set_Visibility(1);
			this.listView.set_Visibility(0);
		}
		public override List<UIElement> GetBkContents()
		{
			if (this.mUiToHideList == null)
			{
				this.mUiToHideList = new List<UIElement>();
				this.mUiToHideList.Add(this.LayoutContent);
			}
			return this.mUiToHideList;
		}
		public override Panel GetChatContainerPanel()
		{
			return this.LayoutRoot;
		}
		protected override void resumeFromChat()
		{
			this.Sessionlist.Focus();
			UIDelayService.getInstance().DoDelayAction();
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (base.PreOnBackKeyPress(cancelEventArgs_0))
			{
				return;
			}
			if (!cancelEventArgs_0.get_Cancel())
			{
				cancelEventArgs_0.set_Cancel(true);
				ItemFlyInAndOutAnimations.TitleFlyOut(this);
				base.get_Dispatcher().BeginInvoke(delegate
				{
					base.GoBack();
				});
			}
		}
		protected override void OnPageOut()
		{
			base.PreOnPageOut();
			this.UnitWatcher();
			Touch.remove_FrameReported(new TouchFrameEventHandler(this.Touch_FrameReported));
		}
		protected override void OnPageForward(Uri targetPage)
		{
			base.PreOnPageForward(targetPage);
		}
		protected override void OnPageBack()
		{
			base.PreOnPageBack();
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/TXBlogRead/UI/TXBlogReadView.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.LayoutContent = (Grid)base.FindName("LayoutContent");
			this.panelTitle = (panelTitle2Line)base.FindName("panelTitle");
			this.canvasProgressBar = (Canvas)base.FindName("canvasProgressBar");
			this.ProgressBar = (PerformanceProgressBar)base.FindName("ProgressBar");
			this.listView = (Grid)base.FindName("listView");
			this.ContentPanel = (StackPanel)base.FindName("ContentPanel");
			this.Sessionlist = (MMListBox)base.FindName("Sessionlist");
			this.NoMemberView = (Grid)base.FindName("NoMemberView");
		}
	}
}
